//
//  HotSongViewController.m
//  GSG
//
//  Created by dllo on 15/4/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HotSongViewController.h"
#import "AFNetworkingTool.h"
#import "AFNetworking.h"
#import "SongBookCell.h"
#import "MusicModel.h"
#import "SongModel.h"
#import "PlayerViewController.h"
#import"PlayListViewController.h"
#import"MBProgressHUD.h"
#import"singleSong.h"
@interface HotSongViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *tableArray;
@property (nonatomic, retain)NSMutableArray *Arr;
@property (nonatomic, copy)NSString *str;
@property (nonatomic, copy)NSString *string;
@property(nonatomic,retain)NSMutableArray *arrModel;
@property(nonatomic,retain)MBProgressHUD*HUD;
@end

@implementation HotSongViewController

- (void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [_Arr release];
    [_arrModel release];
    [_HUD release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"热门歌单";
        self.arrModel = [[NSMutableArray alloc]init];
        self.Arr = [NSMutableArray array];
    }
    return self;
}

#pragma mark -
#pragma mark 创建TableView
- (void)createTableView
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing"]];
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height * 60 / 667) style:UITableViewStylePlain] autorelease];
    self.tableView.rowHeight = 100;
    self.tableView.backgroundColor = [UIColor clearColor];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册
    [self.tableView registerClass:[SongBookCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}

#pragma mark -
#pragma mark 协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return self.tableArray.count;
    return self.arrModel.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    SongBookCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[SongBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
//    MusicModel *model = self.tableArray[indexPath.row];
    SongModel *song=self.arrModel[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1,song.song_name];
    cell.song_nameLabel.text = str;
    cell.singer_nameLabel.text = song.singer_name;
    
    return cell;
}

- (void)data
{
    NSString *str = @"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B%@%5D";
    NSString *a = [NSString stringWithFormat:@"%@", self.Songstr];
    NSString *strNew = [str stringByReplacingOccurrencesOfString:@"%@" withString:a];
//    NSLog(@"___%@",strNew);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *params = [[NSDictionary alloc]init];
    
    [manager GET:strNew parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization  JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray *arr = [dict objectForKey:@"data"];
        
        NSDictionary *dic=arr[0];
        NSMutableArray *arr1=[NSMutableArray array];
        arr1=[dic objectForKey:@"songlist"];
        
        for (int i=0;i<arr1.count;i++){
            self.str=[NSString stringWithFormat:@",%@",[arr1[i] objectForKey:@"song_id"]];
            [self.Arr addObject:self.str];
        }
        self.string = nil;
        NSString *lastString = @"";
        for (NSString *value in self.Arr) {
            self.string = [NSString stringWithFormat:@"%@%@", lastString, value];
            
            lastString = [NSString stringWithFormat:@"%@", self.string];
        }
        [self dataload];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"No");
//        NSLog(@"%@",error);
    }];
    
}
- (void)dataload{
    NSString *str = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@",self.string];
   
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization  JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [dict objectForKey:@"data"];
        for(NSInteger i=0;i<arr.count ;i++){
            SongModel *song=[[SongModel alloc]init];
            NSMutableDictionary *dic=arr[i];
            song.singer_name=[dic objectForKey:@"singer_name"];
            song.song_name=[dic objectForKey:@"song_name"];
            
            NSMutableArray *arr1=[NSMutableArray array];
            arr1=[dic objectForKey:@"url_list"];
            
            NSMutableDictionary *dic1=arr1[0];
            song.url=[dic1 objectForKey:@"url"];
            
            
            [self.arrModel addObject:song];
       
        }

        
        [self.tableView reloadData];
        [_HUD hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"No");
//        NSLog(@"%@",error);
    }];
}
-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
 self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self data];
    [self createTableView];
    
    //小菊花
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    PlayerViewController *pvc=[[PlayerViewController alloc]init];
//    
//    pvc.num=indexPath.row;
//    pvc.arr=self.arrModel;
//    [self.navigationController pushViewController:pvc animated:YES];
//    
//    
//    [pvc release];
    [singleSong shareSingleSong].arrSong=self.arrModel;
    [singleSong shareSingleSong].num=indexPath.row;
    
    NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
    
    [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
